<?php
namespace App\Http\Controllers\Order;

use App\Http\Controllers\Controller;
use App\Models\Order\Product;
use Illuminate\Http\Request;
use Storage;

class ProductController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {

            $rows = $request->get("rows");

            $Product = $this->filters($request, new Product());

            $paginator = $Product->orderBy($request->get("sidx"), $request->get("sord"))->paginate($rows);

            $rows = [];
            foreach ($paginator as $page) {

                $rows[] = $page;
            }

            $data = [
                "rows" => $rows,
                "total" => $paginator->lastPage(),
                "page" => $paginator->currentPage(),
                "records" => $paginator->count(),
            ];

            return response()->json($data);

        } else {

            return view("order/product/index");
        }
    }

    public function edit(Request $request)
    {
        $oper = $request->get("oper");

        if ($oper == "add"){

            $this->validate($request, [
                "discount_type" => "integer|min:0|max:2",
            ]);

            $product = new Product();
            $product->name           = $request->get("name");
            $product->original_price = $request->get("original_price");
            $product->current_price  = $request->get("current_price");
            $product->fee            = $request->get("fee");
            $product->discount_type  = $request->get("discount_type");
            $product->sort           = $request->get("sort");
            $product->image          = $request->get("image");
            $product->spec           = $request->get("spec");
            $product->service        = $request->get("service");
            $product->description    = $request->get("description");
            $product->is_on_sale     = $request->get("is_on_sale");
            $product->save();

        }elseif ($oper == "edit") {
            $this->validate($request, [
                "id" => "int",
                "discount_type" => "integer|min:0|max:2",
            ]);

            $product = Product::find($request->get("id"));
            $product->name           = $request->get("name");
            $product->original_price = $request->get("original_price");
            $product->current_price  = $request->get("current_price");
            $product->fee            = $request->get("fee");
            $product->discount_type  = $request->get("discount_type");
            $product->sort           = $request->get("sort");
            $product->image          = $request->get("image");
            $product->spec           = $request->get("spec");
            $product->service        = $request->get("service");
            $product->description    = $request->get("description");
            $product->is_on_sale     = $request->get("is_on_sale");
            $product->save();

        }elseif ($oper == "del") {
            $product = Product::find($request->get("id"));
            $product->delete();
        } else {
            return response()->json(["success" => false , "message" => "oper error"]);
        }

        return response()->json(["success" => true]);
    }

    public function upload(Request $request) {

        $file = $request->file('upload');
        if($file->isValid()){
            $filename = date('mdHis').uniqid().'.'.$file->getClientOriginalExtension();
            $realPath = $file->getRealPath();
            if(!in_array($file->getClientOriginalExtension(), array('jpg','png','jpeg','gif'))){
                return response()->json(["res" => false,"code"=>'e0001']);
            }
            if($res = Storage::disk('public')->put('img/product/'.$filename,file_get_contents($realPath))){
                if($request->get("CKEditor")) {
                    return "<script>window.parent.CKEDITOR.tools.callFunction(".$request->get("CKEditorFuncNum").",'/resource/img/product/".$filename."','')</script>";
                }
                return response()->json(["res" => '/resource/img/product/'.$filename]);
            }
        }
    }
}